var session = sessionStorage.getItem("session");
var initSel;
if (!session) {
	window.parent.location = "../../login.html";
}
$(function() {
	$.ajax({
		url: IP + "GetSystemParam?&callback=?&flag=1001" + "&session" + session,
		type: "Get",
		dataType: "jsonp",
		jsonp: "callback",
		jsonpCallback: "resultHandler",
		success: function(data) {
			var options = data.vValue.split(",");
			initSel = options[0];
			var sel = [];
			for (var i = 0; i < options.length; i++) {
				var n = [];
				n.push(options[i]);
				switch (options[i]) {
					case "Ping":
						n.push("Ping测试");
						break;
					case "TcpPing":
						n.push("TcpPing测试");
						break;
					case "NsLookUp":
						n.push("DNS测试");
						break;
					case "TraceRoute":
						n.push("路由追踪");
						break;

					case "FtpDownload":
						n.push("FTP下载");
						break;
					case "FtpUpload":
						n.push("FTP上传");
						break;
					case "WebDownload":
						n.push("网页浏览");
						break;
					case "HttpDownload":
						n.push('网页下载');
						break;
					case "RetrieveEmail":
						n.push("邮件接收");
						break;
					case "SendEmail":
						n.push("邮件发送");
						break;
					case "视频测试":
						n.push("视频测试");
						break;
				};
				sel.push(n);
			}
			$("#testType").combobox({
				data: sel,
				valueField: 0,
				textField: 1,
				editable: false,

			})
			init();
		}
	});

})

function init() {
	$("#deviceId").textbox("setValue", '');
	$("#taskType").val("Ping");
	var s = getCurrentTime(7);
	var e = getCurrentTime(0);
	$("#sDate").datebox("setValue", s);
	$("#eDate").datebox("setValue", e);
	$("#testType").combobox("setValue", initSel);
	var Data = getData();
	$("#jqGrid").jqGrid({
		url: IP + 'QueryTaskHistoryInfo?callback=?',
		mtype: "Post",
		datatype: "jsonp",
		postData: Data,
		colModel: [{
			label: '序号',
			sorttype: 'integer',
			name: 'id',
			key: true,
			hidden: true,
			width: 50
		}, {
			label: '参数ID',
			name: 'paramArrayId',
			width: 60,
			hidden: true
		}, {
			label: '探针信息',
			name: 'probeInfo',
			width: 60,
		}, {
			label: '执行方式',
			name: 'executeType',
			editoptions: {
				value: "1:立即执行;2:间隔执行;3:永久执行"
			},
			formatter: 'select',
			editable: true,
			width: 60
		}, {
			label: '开始时间',
			name: 'sDate',
			width: 60
		}, {
			label: '结束时间',
			name: 'eDate',
			width: 60
		}, {
			label: '测试次数',
			name: 'cycleIndex',
			width: 60
		}, {
			label: '时间间隔',
			name: 'timeInterval',
			width: 60
		}, {
			label: '模板ID',
			name: 'taskModelId',
			width: 60,
			hidden: true,
		}, {
			label: '模板名称',
			name: 'taskModelName',
			width: 60
		}, {
			label: '质量评估',
			name: 'zlpgFalg',
			editoptions: {
				value: "0:否;1:是"
			},
			formatter: 'select',
			editable: true,
			width: 60
		}, {
			label: '警告标志',
			name: 'warningFlag',
			editoptions: {
				value: "0:否;1:是"
			},
			formatter: 'select',
			editable: true,
			width: 60
		}, {
			label: '告警次数',
			name: 'wTimes',
			width: 60
		}, {
			label: '成功率一般告警下限',
			name: 'cGeneralLow',
			width: 60
		}, {
			label: '成功率一般告警上限',
			name: 'cGeneralHigh',
			width: 60
		}, {
			label: '成功率严重告警下限',
			name: 'cSevereLow',
			width: 60
		}, {
			label: '成功率严重告警上限',
			name: 'cSevereHigh',
			width: 60
		}, {
			label: '延时一般告警下限',
			name: 'yGeneralLow',
			width: 60
		}, {
			label: '延时一般告警上限',
			name: 'yGeneralHigh',
			width: 60
		}, {
			label: '延时严重告警下限',
			name: 'ySevereLow',
			width: 60,
			hidden: true,
		}, {
			label: '延时严重告警上限',
			name: 'ySevereHigh',
			width: 60,

		}, {
			label: '测试类型',
			name: 'taskType',
			width: 60
		}, {
			label: '任务状态',
			name: 'taskState',
			editoptions: {
				value: "1:正在执行;2:任务挂起;3:任务结束;4:任务删除;"
			},
			formatter: 'select',
			editable: true,
			width: 60,
		}, {
			label: '探针标记',
			name: 'flag',
			width: 60,
			hidden: true,
		}, {
			label: '测试模型',
			name: 'flag2',
			width: 60,
			hidden: true,
		}, {
			label: '参数id',
			name: 'paramArrayId',
			width: 60,
			hidden: true,
		}],
		autowidth: true,
		scroll: true,
		loadonce: true,
		viewrecords: true,
		rowNum: 15,
		page: 1,
		subGrid: true, // set the subGrid property to true to show expand buttons for each row
		subGridRowExpanded: showChildGrid, // javascript function that will take care of showing the child grid
		loadtext: '正在加载数据',
		pager: "#jqGridPager"
	});
	var h = $("body").height();
	$("#jqGrid").jqGrid("setGridHeight", h - 120);
}

function getCurrentTime(t) {
	var curr_time = new Date();
	var strDate = curr_time.getFullYear() + "-";
	strDate += curr_time.getMonth() + 1 + "-";
	strDate += parseInt(curr_time.getDate()) - t + " ";
	return strDate;
}

function taskQuery() {
	var Data = getData();
	$("#jqGrid").jqGrid('setGridParam', {
		url: IP + "QueryTaskHistoryInfo?callback=?",
		mtype: "Post",
		datatype: 'jsonp',
		postData: Data,
	}).trigger("reloadGrid");
}

function getData() {
	var data = {};
	var devid = $("#deviceId").val();
	var taskType = $("#testType").combobox("getValue");
	var sd = $("#sDate").datebox("getValue");
	var ed = $("#eDate").datebox("getValue");
	var zlpgFlag = $("#zlpg").is(":checked") ? "1" : "0";
	var jgFlag = $("#jgFlag").is(":checked") ? "1" : "0";
	if (!taskType || !sd || !ed) {
		$.messager.alert("提醒", "查询条件有误，请重新选择", "info");
		return;
	} else {
		data.session = session;
		data.devid = devid;
		data.sDate = sd;
		data.eDate = ed;
		data.taskType = taskType;
		data.flag = zlpgFlag;
		data.flag2 = jgFlag;
		return data;
	}
}

function showChildGrid(parentRowID, parentRowKey) {
	var childGridID = parentRowID + "_table";
	var childGridPagerID = parentRowID + "_pager";
	// send the parent row primary key to the server so that we know which grid to show
	var childGridURL = IP + "GetProbeTaskDetailList?callback=?&id=" + parentRowKey;
	//childGridURL = childGridURL + "&parentRowID=" + encodeURIComponent(parentRowKey)
	// add a table and pager HTML elements to the parent grid row - we will render the child grid here
	$('#' + parentRowID).append('<table id=' + childGridID + '></table><div id=' + childGridPagerID + ' class=scroll></div>');
	$("#" + childGridID).jqGrid({
		url: childGridURL,
		mtype: "Get",
		datatype: "jsonp",
		page: 1,
		colModel: [{
			label: '创建時間',
			name: 'cTime',
			width: 100,

		}, {
			label: '测试地址',
			name: 'destination',
			width: 100,

		}, {
			label: '设备唯一编码',
			name: 'deviceid',
			width: 100
		}, {
			label: '模板名称',
			name: 'groupName',
			width: 70
		}, {
			label: '开始时间',
			name: 'sDate',
			width: 150,

		}, {
			label: '结束时间',
			name: 'eDate',
			width: 150
		}, {
			label: '间隔时间',
			name: 'meantime',
			width: 60
		}, {
			label: '执行次数',
			name: 'executeTimes',
			width: 70,

		}, {
			label: "任务名称",
			name: 'taskName',
			width: 260
		}, {
			label: '测试类型',
			name: 'taskType',
			width: 80
		}],
		loadonce: true,
		autowidth: true,
		height: 300,
		pager: "#" + childGridPagerID
	});

}